package exer;

import java.util.HashMap;

/**
 * @author tongchen
 * @create 2023-02-09 17:34
 */
public class DeadLocker {
    public static void main(String[] args) {
        //创建两把锁
        Object loker1=new Object();
        Object loker2=new Object();
        //创建两个不同的线程
        Thread t1=new Thread(()->{
            System.out.println(Thread.currentThread().getName()+"试图获取到锁1");
            synchronized(loker1){
                System.out.println(Thread.currentThread().getName()+"获取到锁1");
                try {
                    Thread.sleep(3000);
                    System.out.println(Thread.currentThread().getName()+"试图获取到锁2");
                    synchronized (loker2){
                        System.out.println(Thread.currentThread().getName()+"获取到锁2");
                    }
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }

            }
        },"t1");
        Thread t2=new Thread(()->{
            HashMap<Integer, Integer> integerIntegerHashMap = new HashMap<>();
            System.out.println(Thread.currentThread().getName()+"试图获取到锁1");
            synchronized(loker1){
                System.out.println(Thread.currentThread().getName()+"获取到锁1");
                try {
                    Thread.sleep(3000);
                    System.out.println(Thread.currentThread().getName()+"试图获取到锁2");
                    synchronized (loker2){
                        System.out.println(Thread.currentThread().getName()+"获取到锁2");
                    }
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }

            }
        },"t2");
        t1.start();
        t2.start();

    }
}
